<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="所属门店" class="myform-item">
          <el-select v-model="form.shop_id" placeholder="请选择">
            <el-option
              v-for="item in shopList"
              :key="item.id"
              :label="item.shop_name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择日期">
          <el-date-picker
            v-model="form.strtime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%">
      <template v-for="(item, index) in tableHeader">
        <el-table-column
          v-if="!item.date && item.week"
          :label="item.week"
          :key="index"
          align="center"
          :prop="item.key"
        >
        </el-table-column>
        <el-table-column v-else :label="item.date" :key="index" align="center">
          <el-table-column :label="item.week" align="center" :prop="item.key">
            <div slot-scope="scope">
              <span>{{ scope.row[item.key].test }}</span>
              <i class="el-icon-edit-outline" @click="onEdit(scope.row[item.key],scope.row)"></i>
              <div>
                {{ scope.row[item.key].work_duration }}
              </div>
            </div>
          </el-table-column>
        </el-table-column>
      </template>
    </el-table>
    <pagination />
    <el-dialog :title="detail.name" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false">
      <el-form
        :model="detail"
        ref="detail"
        label-width="80px"
        :inline="false"
        size="normal"
        label-position="top"
      >
        <el-form-item label="">
          <el-input v-model="detail.test"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getApiGetshoplist, postApiApischeduling } from "@/api";
import { getCurrentTime } from "@/utils/time.js";
import pagination from "@/components/pagination.vue";
export default {
  name: "Home",
  components: { pagination },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      form: {
        shop_id: "",
        strtime: "",
      },
      shopList: [],
      tableHeader: [],
      tableData: [],
      detail: "",
    };
  },
  async activated() {
    this.form.strtime = getCurrentTime();
    await this.getApiGetshoplist();
    await this.postApiApischeduling();
  },
  methods: {
    // 编辑
    onEdit(e) {
      console.log("编辑", e);
      this.detail = e;
      this.dialogVisible = true;
    },
    //门店列表
    async getApiGetshoplist() {
      await getApiGetshoplist().then((res) => {
        console.log("门店列表", res);
        this.loading = true;
        this.form.shop_id = res.data[0].id;
        this.shopList = res.data;
      });
    },
    // 排班表
    async postApiApischeduling() {
      this.loading = true;
      let data = this.form;
      await postApiApischeduling(data)
        .then((res) => {
          console.log("排班表", res);
          this.tableHeader = res.data.list;
          this.tableData = res.data.data;
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.postApiApischeduling();
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
